//
//  BIZLiveRoomQuestionCell.m
//  biz
//
//  Created by zhangfuwei on 2017/3/31.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZLiveRoomQuestionCell.h"
#import "Utility.h"
#import "UIImage+Extension.h"
#import "Dialogue.h"    

#define kContentFontSize        16.0f   //内容字体大小
#define kPadding                5.0f    //控件间隙
#define kEdgeInsetsWidth       20.0f   //内容内边距宽度
#define kWidth [UIScreen mainScreen].bounds.size.width //获取设备的物理宽度
#define kHeight [UIScreen mainScreen].bounds.size.height //获取设备的物理高度
static CGFloat kViewMargin = 15;
static CGFloat kTitleLabH = 20;
static NSString *kCellIdentifier = @"BIZLiveRoomQuestionCell";
static NSString *kReplyContent = @"—— 您好，您的问题已收悉，我们会尽快给您回复 ——";

@interface BIZLiveRoomQuestionCell ()

/** 名称 */
@property (nonatomic, retain) UILabel      *titleLabel;
/** 背景图 */
@property (nonatomic, retain) UIView      *bgView;
/** 文字内容视图 */
@property (nonatomic, retain) UILabel       *contentLabel;
/** 默认回复文案 */
@property (nonatomic, strong) UILabel *replyLabel;
/** 内容是否超过一行 */
@property (nonatomic, assign) BOOL moreThan1;
/** 数组 */
@property (nonatomic, strong) NSArray *dialoguesArr;


@end

@implementation BIZLiveRoomQuestionCell

#pragma mark - life cirle method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier dialoguesArr:(NSArray *)dialoguesArr
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _dialoguesArr = dialoguesArr;
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initSubViews];

    }
    return self;
}

- (void)initSubViews
{
    //    //初始化正文视图
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.4;
    _bgView.layer.cornerRadius = 20;
    _bgView.clipsToBounds = YES;
    
    //初始化名称视图
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FONT(CHINESE_FONT_NAME, 14);
    _titleLabel.numberOfLines = 1;
    _titleLabel.backgroundColor = [UIColor clearColor];
    
    //初始化正文视图
    _contentLabel = [[UILabel alloc] init];
//    _contentLabel.font = FONT(CHINESE_FONT_NAME, 14);
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.numberOfLines = 0;
    
    
    _replyLabel = [UILabel new];
    _replyLabel.numberOfLines = 0;
    _replyLabel.backgroundColor = CCClearColor;
    _replyLabel.textAlignment = NSTextAlignmentLeft;
    _replyLabel.lineBreakMode = NSLineBreakByCharWrapping;

    [self.contentView addSubview:_bgView];
    [self.contentView insertSubview:_titleLabel aboveSubview:_bgView];
    [self.contentView insertSubview:_contentLabel aboveSubview:_bgView];
    [self.contentView addSubview:_replyLabel];

    
    [self setFirstDialogueViewWithArr:_dialoguesArr];

    [self setSecondDialogueViewWithArr:_dialoguesArr];
    
    // 暗色背景视图
    CGFloat bgViewW = Main_Screen_Width - kViewMargin *2;
    CGFloat bgViewH = [self heightOfCellWithDialoguesArr:_dialoguesArr];
    _bgView.frame = CGRectMake(kViewMargin, 0, bgViewW, bgViewH-kViewMargin);

}


#pragma mark - class method


+(instancetype)cellWithTableView:(UITableView *)tableView dialoguesArr:(NSArray *)dialoguesArr {
    BIZLiveRoomQuestionCell *cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier dialoguesArr:dialoguesArr];
    return cell;
}

#pragma mark - setter and getter

- (void)setFirstDialogueViewWithArr:(NSArray *)arr {
    
    Dialogue *dialogue = [arr objectAtIndex:0];

    CGFloat maxTextWidth =  Main_Screen_Width - kViewMargin *5;
    //  标题lable
    NSMutableAttributedString *titleAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",dialogue.username] attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14),NSForegroundColorAttributeName:COLOR_WORD_YELLOW_OTHER}];
    
    CGSize titleTextSize = [titleAttStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, kTitleLabH)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                     context:nil].size;
    
    CGFloat titlLabW = titleTextSize.width;
    _titleLabel.attributedText = titleAttStr;
    _titleLabel.frame = CGRectMake(kViewMargin *2, kViewMargin, titlLabW, kTitleLabH);
    
    
    //  内容label
    CGFloat contentLabW = maxTextWidth - titlLabW;

    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:dialogue.msg attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];

    CGSize contentSize = [attrStr boundingRectWithSize:CGSizeMake(contentLabW, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;
    
    _contentLabel.attributedText = attrStr;
    _contentLabel.frame = CGRectMake(kViewMargin *3 + titlLabW, kViewMargin, contentLabW, contentSize.height);
    
    // 问答回复
    
    NSMutableAttributedString *textAttri1 = [[NSMutableAttributedString alloc] initWithString:kReplyContent attributes:@{NSForegroundColorAttributeName:COLOR_WORD_YELLOW_SELF,NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    
    CGSize replySize = [textAttri1 boundingRectWithSize:CGSizeMake(maxTextWidth, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                               context:nil].size;
    
    _replyLabel.attributedText = textAttri1;
    
    _replyLabel.frame = CGRectMake(kViewMargin*2, kViewMargin*2+contentSize.height, replySize.width, replySize.height);
}

- (void)setSecondDialogueViewWithArr:(NSArray *)arr {
    UIView *viewBase = nil;
    for(int i = 1;i < [arr count];i++) {
        Dialogue *dialogue = [arr objectAtIndex:i];
        UIView *viewTop = [UIView new];
        // MARK:隐藏回复分割线
//        viewTop.backgroundColor = CCRGBColor(232,232,232);
        
        viewTop.backgroundColor = CCClearColor;
        [self.contentView addSubview:viewTop];
        if(viewBase == nil) {
            [viewTop mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView).offset(CCGetRealFromPt(130));
                make.top.mas_equalTo(_replyLabel.mas_bottom).offset(CCGetRealFromPt(16));
                make.size.mas_equalTo(CGSizeMake(CCGetRealFromPt(590), 1));
            }];
        } else {
            viewTop = viewBase;
        }
        float textMaxWidth = Main_Screen_Width - kViewMargin*5;
        NSString *text = [[dialogue.username stringByAppendingString:@": "] stringByAppendingString:dialogue.msg];
        NSMutableAttributedString *textAttri1 = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:CHINESE_SYSTEM(14)}];
        
        [textAttri1 addAttribute:NSForegroundColorAttributeName value:COLOR_WORD_BLUE_ANCHOR range:NSMakeRange(0, [dialogue.username stringByAppendingString:@": "].length)];
        NSInteger fromIndex = [dialogue.username stringByAppendingString:@": "].length;
        [textAttri1 addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(fromIndex,text.length - fromIndex)];
        
        CGSize textSize = [textAttri1 boundingRectWithSize:CGSizeMake(textMaxWidth, CGFLOAT_MAX)
                                                   options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                   context:nil].size;
        UIView *viewBg = [UIView new];
        viewBg.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:viewBg];
        [viewBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.contentView);
            make.top.mas_equalTo(viewTop.mas_bottom);
            make.height.mas_equalTo(textSize.height + CCGetRealFromPt(10) + CCGetRealFromPt(20));
        }];
        
        UILabel *contentLabel = [UILabel new];
        contentLabel.numberOfLines = 0;
        contentLabel.backgroundColor = CCClearColor;
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        contentLabel.attributedText = textAttri1;
        [viewBg addSubview:contentLabel];
        
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).offset(kViewMargin*2);
            make.centerY.mas_equalTo(viewBg).offset(-1);
            make.size.mas_equalTo(textSize);
        }];
        
        UIView *viewBottom = [UIView new];
        viewBottom.backgroundColor = CCClearColor;
        [self.contentView addSubview:viewBottom];
        [viewBottom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(viewTop);
            make.top.mas_equalTo(viewBg.mas_bottom);
            make.height.mas_equalTo(1);
        }];
        
        viewBase = viewBottom;
    }
    
}

+ (CGFloat)heightOfCellWithDialoguesArr:(NSArray *)dialoguesArr {
  
    return [[[self alloc] init] heightOfCellWithDialoguesArr:dialoguesArr];
}

- (CGFloat)heightOfCellWithDialoguesArr:(NSArray *)dialoguesArr {
    Dialogue *firstDialogue = [dialoguesArr objectAtIndex:0];
    float textMaxWidth = Main_Screen_Width - kViewMargin *5;

    CGFloat H = 0;
    
    //  计算标题label宽度
    NSMutableAttributedString *attrStr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",firstDialogue.username] attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    CGSize titleSize = [attrStr1 boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,kTitleLabH )
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                              context:nil].size;
    CGFloat titlLabW = titleSize.width;
    
    //  计算内容label宽度
    CGFloat contentLabW = textMaxWidth - titlLabW;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:firstDialogue.msg attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    CGSize contentSize = [attrStr boundingRectWithSize:CGSizeMake(contentLabW, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;
    
    
    // 自动回复
    NSMutableAttributedString *textAttri1 = [[NSMutableAttributedString alloc] initWithString:kReplyContent attributes:@{NSForegroundColorAttributeName:COLOR_WORD_YELLOW_SELF,NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    
    CGSize replySize = [textAttri1 boundingRectWithSize:CGSizeMake(textMaxWidth, CGFLOAT_MAX)
                                                options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                context:nil].size;


    //  cell 整体高度
    H = contentSize.height + kViewMargin *3 + 10 + replySize.height;
    
    NSInteger baseHeight = -1;
    for(int i = 1;i < [dialoguesArr count];i++) {
        Dialogue *dialogue = [dialoguesArr objectAtIndex:i];
        if(baseHeight == -1) {
            H += 2;
        }
        
        NSString *text = [[dialogue.username stringByAppendingString:@": "] stringByAppendingString:dialogue.msg];
        NSMutableAttributedString *textAttri1 = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:CHINESE_SYSTEM(14)}];

        
        CGSize textSize = [textAttri1 boundingRectWithSize:CGSizeMake(textMaxWidth, CGFLOAT_MAX)
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil].size;
        H += (textSize.height + CCGetRealFromPt(10) + CCGetRealFromPt(20));
        H += 2;
        baseHeight = 0;
    }
    
    return H;
}
@end
